<?php
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;

class DefaultDb_Repositories_ProductRepository extends EntityRepository
{    
    public function addProduct($data)
    {
        $product = new DefaultDb_Entities_Product();

        if($product !== false)
        {
            foreach($data as $key => $value)
            {
                try
                {
                    $aux = 'set'.ucfirst($key);
                    $product->$aux($value);
                }
                catch(Exception $exc)
                {
                }
            }

            // algunos datos que no dejamos que controle el usuario
            //$product->setVariantsUse(DefaultDb_Entities_Product::VARIANTS_NOT_USE);
            $product->setStatus(DefaultDb_Entities_Product::STATUS_ACTIVE);
            $product->setVisible(DefaultDb_Entities_Product::VISIBLE_YES);
            
//            $product->creation_date = date('Y-m-d H:i:s');
            
            $em = $this->getEntityManager();
            $em->persist($product);
            $em->flush();

        }
        return $product;
        
    }

    public function updateProduct($productId, $data)
    {
        $em = $this->getEntityManager();
        $product = $this->find($productId);
        $class = $this->getClassName();
        $reg = false;

        if($product !== false && $product instanceof $class )
        {
            foreach($data as $key => $value)
            {
                try
                {
                    $aux = 'get'.ucfirst($key);
                    $current = $product->$aux();
                    if($current != $value)
                    {
                        $aux = 'set'.ucfirst($key);
                        $product->$aux($value);
                    }
                }
                catch(Exception $exc)
                {
                }
            }
            $em->persist($product);
            $reg = $em->flush();
            
        }
        return $reg;

    }

}